/**
 * 
 */
package com.audi.box.server.handler;

import android.util.Log;

import com.audi.box.controller.IController;
import com.audi.box.controller.SceneController;
import com.audi.box.server.message.HandlerException;
import com.audi.box.server.message.ObjectCombine;
import com.audi.dto.SceneDeviceItem;
import com.audi.dto.SceneItem;

/**
 * @author HuyPVA
 * 
 */
public class SceneHandler extends Handler {
	private static SceneHandler sceneHandler;
	private static Object sceneObj = new Object();

	/**
	 * @param controller
	 */
	private SceneHandler(IController controller) {
		super(controller);
	}

	public static SceneHandler getInstance(IController controller) {
		if (sceneHandler == null) {
			synchronized (sceneObj) {
				if (sceneHandler == null) {
					sceneHandler = new SceneHandler(controller);
				}
				return sceneHandler;
			}
		} else {
			return sceneHandler;
		}
	}

	/*
	 * Get DeviceController
	 */
	public SceneController getSceneController() {
		return (SceneController) getController();
	}

	/*
	 * Process message by action
	 */
	@Override
	public Object process(int actionId, String data) throws HandlerException {
		Object result = null;
		SceneItem sceneItem = null;
		SceneDeviceItem sceneDeviceItem = null;

		switch (actionId) {
		case ADD_SCENE:
			sceneItem = getGson().fromJson(data, SceneItem.class);
			result = getSceneController().addScene(sceneItem);
			break;
		case UPDATE_SCENE:
			sceneItem = getGson().fromJson(data, SceneItem.class);
			result = getSceneController().updateScene(sceneItem);
			break;
		case DELETE_SCENE:
			sceneItem = getGson().fromJson(data, SceneItem.class);
			result = getSceneController().deleteScene(sceneItem);
			break;
		case GET_SCENE_BY_ID:
			int sceneId = getGson().fromJson(data, Integer.class);
			result = getSceneController().getSceneById(sceneId);
			break;
		case GET_LIST_SCENE:
			result = getSceneController().getSceneList();
			break;
		case CHECK_SCENE_EXIST:
			sceneItem = getGson().fromJson(data, SceneItem.class);
			result = getSceneController().isExist(sceneItem);
			break;
		case ADD_SCENE_DEVICE:
			sceneDeviceItem = getGson().fromJson(data, SceneDeviceItem.class);
			result = getSceneController().addSceneDeviceItem(sceneDeviceItem);
			break;
		case GET_SCENE_DEVICE:
			ObjectCombine objectCombine = getGson().fromJson(data,
					ObjectCombine.class);
			String strSceneId = objectCombine.getObj1();
			String strDeviceId = objectCombine.getObj2();
			result = getSceneController()
					.getSceneDevice(Integer.parseInt(strSceneId),
							Integer.parseInt(strDeviceId));
			break;
		case GET_SCENE_DEVICE_LIST:
			result = getSceneController().getSceneDeviceList();
			break;			
		case GET_SCENE_LIST_BY_PLACE_ID:
			int placeId = getGson().fromJson(data, Integer.class);
			result = getSceneController().getSceneListByPlaceId(placeId);
			break;
		default:
			Log.e("AudiBox", "[SceneHandler.java] Function not support: "
					+ actionId);
			throw new HandlerException("Function " + actionId +" not found!!!");
		}
		return result;

	}

}
